package com.hqyj.springbootday03.modules.test.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hqyj.springbootday03.modules.common.entity.AbstractEntity;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "test_student")
public class Student extends AbstractEntity {

    private String studentName;
    private int age;

    /**
     * OneToOne：一对一关系中，有外键方使用 JoinColumn 注解，另一方使用 mappedBy 属性（可选）
     * targetEntity：目标实体
     * cascade：联级操作
     * fetch：加载数据策略
     * JoinColumn
     * name 对应 test_student 表中 card_id 外键列
     */
    @OneToOne(targetEntity = Card.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "card_id", unique = true)
    private Card card;

    /**
     * ManyToMany，一方使用 JoinTable 注解，另一方配置 mappedBy 属性
     * cascade：联级操作
     * fetch：加载数据策略
     * JsonIgnore：不序列化该字段，避免无限递归
     */
    @ManyToMany(mappedBy = "students", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonIgnore
    private List<Clazz> clazzes;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }

    public List<Clazz> getClazzes() {
        return clazzes;
    }

    public void setClazzes(List<Clazz> clazzes) {
        this.clazzes = clazzes;
    }
}
